from eden.evo.chromosome import Chromosome
import doctest
import random

class Cell:
    __ID = 0
    
    def __init__(self, translator):
        Cell.__ID += 1
        self.id = Cell.__ID
        self.generation = 0
        self.__translator = translator
        self.reset()
        
    def gettranslator(self):
        return self.__translator
    
    def reset(self):
        self.fitness = 0.0
        self.hitratio = 0.0
        
class NonsexualCell(Cell):
    def __init__(self, chromosome, translator):
        Cell.__init__(self, translator)
        self.chromosome = chromosome
        
    @staticmethod
    def create_null_cell(len, translator):
        chromosome = Chromosome.create_null_chromosome(len)
        return NonsexualCell(chromosome, translator)
        
    def getchromosome(self):
        return self.chromosome
    
    def run(self, context):
        protein = self.gettranslator().translate(self.chromosome)
        protein.run(context)

class SexualCell(Cell):
    def __init__(self, left, right, translator):
        Cell.__init__(self, translator)
        self.__left = left
        self.__right = right
        
    @staticmethod
    def create_null_cell(len, translator):
        left = right = Chromosome.create_null_chromosome(len)
        return SexualCell(left, right, translator)
        
    def getleft(self):
        return self.__left
    
    def getright(self):
        return self.__right
    
    def run(self, context):
        chromosome = random.choice((self.__left, self.__right))
        protein = self.gettranslator().translate(chromosome)
        protein.run(context)
        
if __name__ == '__main__':
    doctest.testmod()